package lnkwe.sys.ueditor;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import java.io.*;


/**
 * 配置管理器
 *
 * @author hancong03@baidu.com
 */
public final class ConfigManager {
    private final String rootPath;
    private final String originalPath = "static/js/plugins/ueditor/jsp";
    private final String contextPath;
    private String parentPath = null;
    private static final String configFileName = "config.json";
    private JSONObject jsonConfig = new JSONObject();

    private ConfigManager(String rootPath, String contextPath, String uri) throws Exception {
        rootPath = rootPath.replace("\\", "/");
        this.rootPath = rootPath;
        this.contextPath = contextPath;

        /*if (contextPath.length() > 0) {
            this.originalPath = this.rootPath + uri.substring(contextPath.length());
        } else {
            this.originalPath = this.rootPath + uri;
        }*/
        this.initEnv();

    }

    public static ConfigManager getInstance(String rootPath, String contextPath, String uri) {

        try {
            return new ConfigManager(rootPath, contextPath, uri);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

    }

    public JSONObject getAllConfig() {
        return this.jsonConfig;
       /* JSONObject jsonConfig = new JSONObject();
        JSONArray allowFiles = new JSONArray();
        jsonConfig.put("imageActionName", "uploadimage");
        jsonConfig.put("imageFieldName", "upfile");
        jsonConfig.put("imageMaxSize", 2048000);
        allowFiles.add(".png");
        allowFiles.add(".jpg");
        allowFiles.add(".jpeg");
        allowFiles.add(".gif");
        allowFiles.add(".bmp");
        jsonConfig.put("imageAllowFiles", allowFiles);
        jsonConfig.put("imageCompressEnable", true); *//* 是否压缩图片,默认是true *//*
        jsonConfig.put("imageCompressBorder", 1600); *//* 图片压缩最长边限制 *//*
        jsonConfig.put("imageInsertAlign", "none"); *//* 插入的图片浮动方式 *//*
        jsonConfig.put("imageUrlPrefix", "");
        return jsonConfig;*/
    }

    private void initEnv() throws FileNotFoundException, IOException {
        String filePath = this.rootPath  + this.originalPath;
        File file = new File(filePath);
        if (!file.isAbsolute()) {
            file = new File(file.getAbsolutePath());
        }
        this.parentPath = file.getAbsolutePath();
        String configContent = this.readFile(this.getConfigPath());
        try {
            JSONObject jsonConfig =JSON.parseObject(configContent);
            this.jsonConfig = jsonConfig;
        } catch (Exception e) {
            this.jsonConfig = null;
        }

    }

    private String readFile(String path) throws IOException {

        StringBuilder builder = new StringBuilder();

        try {

            InputStreamReader reader = new InputStreamReader(new FileInputStream(path), "UTF-8");
            BufferedReader bfReader = new BufferedReader(reader);

            String tmpContent = null;

            while ((tmpContent = bfReader.readLine()) != null) {
                builder.append(tmpContent);
            }

            bfReader.close();

        } catch (UnsupportedEncodingException e) {

        }

        return this.filter(builder.toString());

    }

    private String filter(String input) {
       return input.replaceAll("/\\*[\\s\\S]*?\\*/", "");
    }
    public boolean valid () {
        return this.jsonConfig != null;
    }
    private String getConfigPath() {
        return this.parentPath + File.separator + ConfigManager.configFileName;
    }
}
